AddCSLuaFile( "cl_init.lua" );
AddCSLuaFile( "shared.lua" );
include("shared.lua");


ENT.BarrierType = 1

util.PrecacheSound("ambient/levels/labs/electric_explosion5.wav");

ENT.NotAllowedEnts = {
"prop_",
"rpg_missile",
"npc_satchel",
"weapon_",
"npc_",
"npc_grenade_frag",
"npc_grenade_bugbait",
"grenade_ar2",
"crossbow_bolt",
"func_physbox",
"item_",
--"bam_melon_missile",
--"bam_melon",
"sent_ball",
"combinemine"}

function ENT:Initialize()
	self.Entity:DrawShadow( false );
	self.Entity:SetSolid( SOLID_NONE );
end

function ENT:KeyValue( key, value )
	if ( key == "type" ) then
		self.BarrierType = value;
		--Msg("(ENT) (".. type(value) ..") Barrier Type: ".. value .."\n");
	end
end

function ENT:EntNotAllowed( ent )
	
	if self.Entity:GetOwner():IsValid() and (ent == self.Entity:GetOwner()) then
		return false;
	end
	
	if ent:IsPlayer() then
		local owner = self.Entity:GetOwner();
		local res, i, x, y = ent:HasItem( "combinebarriercard" );
		if ( (owner and owner:IsValid() and owner == ent) or (ent:IsCombineDefense()) or (ent:HasCPModel()) or (res) ) then
			return false;
		else
			return true;
		end
	end
	
	local allowed = false;
	
	for k, v in pairs( self.NotAllowedEnts ) do
		if ent:GetClass():find( v ) then
			allowed = true;
			break;
		end
	end
	
	return allowed;
end


function ENT:Think()
	local firstpos = self.Entity:GetNWVector( "MinVec" )
	local secondpos = self.Entity:GetNWVector( "MaxVec" );

	
	local CornerOne = Vector( math.min( firstpos.x, secondpos.x ), math.min( firstpos.y, secondpos.y ), math.min( firstpos.z, secondpos.z ) );
	local CornerTwo = Vector( math.max( firstpos.x, secondpos.x ), math.max( firstpos.y, secondpos.y ), math.max( firstpos.z, secondpos.z ) );
	
	local entities = ents.FindInBox( CornerOne, CornerTwo );
	
	for k, ent in pairs( entities ) do
		if !ent:IsWorld() then
			if ent:IsPlayer() and ent:Alive() then
				if tonumber(self.BarrierType) == 1 then
					if self:EntNotAllowed( ent ) then
						ent:SetVelocity( ent:GetVelocity() * -5 );
						self:DoShock( ent );
					end
					
				elseif tonumber(self.BarrierType) == 2 then
						ent:Kill();
				end
				
			else				
				if self:EntNotAllowed(ent) then -- Only do something if the entity isn't allowed to go through.
					
					if ent:IsPlayerHolding() then
						ent:Remove();
						self:DoShock( ent );
					end
					
					if --[[ent:GetClass() == "bam_melon_missile" or]] ent:GetClass() == "crossbow_bolt" then
						ent:SetAngles( ent:GetAngles() + Angle(180, 180, 180) );
					end
					
					
					if ent:GetPhysicsObject():IsValid() then
						if util.IsValidRagdoll( ent:GetModel() ) then
							for i = 1, ent:GetPhysicsObjectCount() do
								local phys = ent:GetPhysicsObjectNum(i - 1);
								phys:SetVelocity( phys:GetVelocity() * -2.5 );
							end
							
							self:DoShock( ent );
							
						else
							if ent:IsVehicle() and ent:GetDriver() == self.Entity:GetOwner() then
								// LOL NOTHING O_O
							else
								local phys = ent:GetPhysicsObject();
								if phys and phys:IsValid() and phys:IsMoveable() then
									phys:SetVelocity( phys:GetVelocity() * -2.5 );
									self:DoShock( ent );
								end
							end
						end
					else -- If it isn't a valid prop or ragdoll or physics object, then lets try and set it's velocity without the physobj.
						ent:SetVelocity( ent:GetVelocity() * -2.5 );
						
						self:DoShock( ent );
					end
				end
			end
		end
	end
	
	self.Entity:NextThink( CurTime() ); -- Think fast! :O
	return true;
end

local NextShockTime = 0
local ShockDelay = 0.5
function ENT:DoShock( ent )
	ent:EmitSound("ambient/levels/labs/electric_explosion5.wav");
	
	--Msg( ent:GetClass() .."\n" );
	
	local owner = self.Entity:GetOwner();
	if ent:IsPlayer() and owner and owner:IsValid() then
		ent:TakeDamage( 5, owner );
	end
end

